package cn.com.controler.Admin;
import cn.com.controler.Util.JDBCUtil;
import cn.com.controler.Util.TimeTr;
import cn.com.entity.Admin;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class AdminDAOTemplete {

    //添加数据
    public static int insertdata(String str,Object[]arr)
    {
        Connection coll = null;
        PreparedStatement statement = null;
        try {
            coll = JDBCUtil.getConnection();
            statement = coll.prepareStatement(str);
            //设置参数
            for (int i = 0; i < arr.length ; i++) {
                statement.setObject(i+1,arr[i]);
            }
            //执行
            return statement.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(coll,statement,null);
        }
        return 0;
    }


    //查询数据
    public static List selectdata(String str, Object[]arr)
    {
        Connection coll = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List list = new ArrayList();
        Date date1;
        String date;
        try {
            coll = JDBCUtil.getConnection();
            statement = coll.prepareStatement(str);
            //设置参数
            for (int i = 0; i < arr.length ; i++) {
                statement.setObject(i+1,arr[i]);
            }
            //执行
            resultSet = statement.executeQuery();
            while (resultSet.next()){
                Admin admin = new Admin();
                date1 = resultSet.getDate("AdminRegisterTime");
                date = TimeTr.Date_String1(date1);
                admin.setAdminId(resultSet.getInt("AdminId"));
                admin.setAdminUsername(resultSet.getString("AdminUsername"));
                admin.setAdminPassword(resultSet.getString("AdminPassword"));
                admin.setAdminEmail(resultSet.getString("AdminEmail"));
                admin.setAdminRegisterTime(date);
                admin.setAdminRoleType(resultSet.getInt("AdminRoleType"));
                list.add(admin);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(coll,statement,resultSet);
        }
        return null;
    }
}
